export default {
	data() {
		return {
			//数据
			validate: {}, //验证对象
			announcement: {} ,//通知公告信息
      tempName: '',
			uuidid:""
		}
	},
	created() { //调用方法
		let that = this;

	},
	mounted() {
		let that = this;
		UE.delEditor('editor');
		var ue = UE.getEditor('editor', {
			maximumWords: 10000
		});
    ue.addListener( 'contentChange', function( editor ) {
      $("textarea[name='content']").val(ue.getContent());
      $("textarea[name='content']").valid();
    });
		UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
		UE.Editor.prototype.getActionUrl = function(action) {
			if(action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
				return that.urls.uploadUediter;
			} else if(action == 'uploadvideo') { //视频
				return that.urls.uploadUediter;
			} else if(action == 'uploadfile') { //附件
				return that.urls.uploadUediter;
			} else {
				return this._bkGetActionUrl.call(this, action);
			}
		};

		let uuid = that.get("theAnnouncementid");
		this.uuidid=that.get("theAnnouncementid");
		if(uuid != null && uuid != "") {
			that.$post(
					that.urls.getAnnouncement, {
						"uuid": uuid
					}
				)
				.then(function(res) {
					if(res.success) {
						that.announcement = res.data;
						if (res.data != null) {
              that.tempName = res.data.name;
            }
						ue.ready(function() {
							ue.setContent(that.announcement.content);
						});
					}
				})
		}
		//校验名称重复
		$.validator.addMethod("checkNameRepeat", function(value, element, params) {
			let flg = false;
			if(that.announcement.id != null && (value == that.tempName)) {
				flg = true;
			} else {
				$.ajax({
					type: "POST",
					async: false,
					url: that.urls.checkAnnouncementName,
					data: JSON.stringify({
						"name": value
					}),
					dataType: "json",
					contentType: "application/json",
					success: function(data) {
						if(data.success) {
							flg = !data.data;
						}
					}
				});
			}
			return this.optional(element) || flg;
		}, "该标题已存在！");
		//加载表单验证
		that.validate = $("#announcementForm").validate({
			ignore: [],
			rules: {
				name: { // 通知公告名称
					required: true,
					maxlength: 50,
					checkNameRepeat: true
				},
				content: { // 通知公告内容
					required: true
				}
			},
			messages: {
				name: { // 通知公告名称
					required: "请输入标题",
					maxlength: "标题最多只能输入50个字"
				},
				content: { // 通知公告内容
					required: "请输入内容"
				}
			},
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
		});
	},
	methods: { //写函数方法
		//保存
		saveData: function() {
			let that = this;
			let flg = that.validate.form();
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.addAnnouncement,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							that.announcement = res.data;
							msg = "发布成功！";
							layer.msg(msg);
							that.backAnnouncements();
						} else {
							msg = "发布失败！";
							layer.msg(msg);
						}

					})
			}
		},
		//修改
		updateData: function() {
			let that = this;
			let flg = that.validate.form();
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.updateAnnouncement,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							msg = "保存成功！";
							layer.msg(msg);
							that.backAnnouncements();
						} else {
							msg = "保存失败！";
							layer.msg(msg);
						}
					})
			}
		},
		/*跳转到通知公告列表*/
		backAnnouncements: function() {
			var that = this;
			that.$router.push({
				path: "announcements",
			})
		},
	}
}
